package com.zzl.study.auth.service.impl;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzl.study.auth.dao.RegisteredClientMapper;
import com.zzl.study.auth.domain.RegisteredClient;
import com.zzl.study.auth.domain.converter.RegisteredClientConverter;
import com.zzl.study.auth.domain.converter.RegisteredClientDtoConverter;
import com.zzl.study.auth.domain.dto.RegisteredClientDto;
import com.zzl.study.auth.service.RegisteredClientService;
import jakarta.annotation.Resource;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: zhangzl
 * @date: 2023/12/8 14:07
 * @version: 1.0
 * @description: 注册客户端Service实现类
 */
@Service
public class RegisteredClientServiceImpl implements RegisteredClientService {

    @Resource
    private RegisteredClientMapper mapper;

    @Resource
    private RegisteredClientConverter registeredClientConverter;

    @Resource
    private RegisteredClientDtoConverter registeredClientDtoConverter;

    @Override
    public PageInfo<RegisteredClientDto> selectRegisteredClientPageList(String clientName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<RegisteredClient> registeredClientList = this.mapper.selectList(clientName);
        if (!CollectionUtils.isEmpty(registeredClientList)) {
            List<RegisteredClientDto> registeredClientDtoList = registeredClientList.stream().map(e -> this.registeredClientDtoConverter.convert(e)).toList();
            return new PageInfo<>(registeredClientDtoList);
        }
        return new PageInfo<>(new ArrayList<>());
    }

    @Override
    public String createRegisteredClient(RegisteredClientDto registeredClientDto) {
        RegisteredClient registeredClient = this.registeredClientConverter.convert(registeredClientDto);
        registeredClient.setId(IdUtil.getSnowflakeNextIdStr());
        registeredClient.setClientIdIssuedAt(new Timestamp(new Date().getTime()));
        String secretKey = new BCryptPasswordEncoder().encode(registeredClient.getClientId());
        registeredClient.setClientSecret(secretKey);
        this.mapper.insert(registeredClient);
        return secretKey;
    }

    @Override
    public RegisteredClientDto selectRegisteredClientById(String id) {
        RegisteredClient registeredClient = this.mapper.selectById(id);
        return registeredClient == null ? null : this.registeredClientDtoConverter.convert(registeredClient);
    }

    @Override
    public void updateRegisteredClient(RegisteredClientDto registeredClientDto) {
        RegisteredClient registeredClient = this.registeredClientConverter.convert(registeredClientDto);
        this.mapper.update(registeredClient);
    }

    @Override
    public void deleteRegisteredClientByIds(String[] ids) {
        this.mapper.deleteByIds(ids);
    }

}
